using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ActiveObjectAttackEvent : MonoBehaviour
{
    // Start is called before the first frame update

    private List<GameObject> attackList;
    private bool attackEnable;
    private string attackObjectType;


    void Start()
    {
        resetHitObjectList();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void setEnable(bool enable,string atkObjType = "")
    {
        attackEnable = enable;
        attackObjectType = atkObjType;
    }

    public void resetHitObjectList()
    {
        attackList = new List<GameObject>();
    }

    public AttackObjectTempVector onInAreaAttack(float xoffset,float yoffset,float width,float height)
    {

        ActiveObject obj = gameObject.GetComponent<ActiveObject>();

        if (!attackEnable)
            return null;

        Vector2 startPoint = obj.getCurrentPos() + new Vector2(Com.getDistancePos(0f, obj.getDirection(), xoffset), yoffset);

        List<RaycastHit2D> tempHitList = new List<RaycastHit2D>();

        RaycastHit2D[] hit = Physics2D.RaycastAll(startPoint,
            Com.directionToVector2(obj.getDirection()), width, LayerMask.GetMask(attackObjectType));

        foreach (RaycastHit2D i in hit)
        {
            if (!tempHitList.Contains(i))
                tempHitList.Add(i);
        }

        hit = Physics2D.RaycastAll(startPoint + new Vector2(0, height / 4),
        Com.directionToVector2(obj.getDirection()), width, LayerMask.GetMask(attackObjectType));

        foreach (RaycastHit2D i in hit)
        {
            if (!tempHitList.Contains(i))
                tempHitList.Add(i);
        }

        hit = Physics2D.RaycastAll(startPoint + new Vector2(0, height / 2),
        Com.directionToVector2(obj.getDirection()), width, LayerMask.GetMask(attackObjectType));

        foreach (RaycastHit2D i in hit)
        {
            if (!tempHitList.Contains(i))
                tempHitList.Add(i);
        }

        hit = Physics2D.RaycastAll(startPoint + new Vector2(0, (height * 3) / 4),
        Com.directionToVector2(obj.getDirection()), width, LayerMask.GetMask(attackObjectType));

        foreach (RaycastHit2D i in hit)
        {
            if (!tempHitList.Contains(i))
                tempHitList.Add(i);
        }

        hit = Physics2D.RaycastAll(startPoint + new Vector2(0, height),
        Com.directionToVector2(obj.getDirection()), width, LayerMask.GetMask(attackObjectType));

        foreach (RaycastHit2D i in hit)
        {
            if (!tempHitList.Contains(i))
                tempHitList.Add(i);
        }

        List<ActiveObject> hitList = new List<ActiveObject>();

        foreach (RaycastHit2D hitObj in tempHitList)
        {

            if (!attackList.Contains(hitObj.collider.gameObject))
            {
                ActiveObject active = hitObj.collider.gameObject.GetComponent<ActiveObject>();
                if (active && active.isDamageable())
                {
                    hitList.Add(active);
                    attackList.Add(hitObj.collider.gameObject);
                }
            }
        }

        AttackObjectTempVector atk = new AttackObjectTempVector(hitList);
        return atk;


        /*
        Vector2 startPoint = obj.getCurrentPos() + new Vector2(Com.getDistancePos(0f,obj.getDirection(),xoffset), yoffset);
        Vector2 endPoint = new Vector2(Com.getDistancePos(startPoint.x, obj.getDirection(), width), startPoint.y + height + obj.getCollider().bounds.size.y);
 
        Collider2D hit = Physics2D.OverlapArea(startPoint, endPoint, LayerMask.GetMask(attackObjectType));
        if (hit && !attackList.Contains(hit.gameObject))
        {
            attackList.Add(hit.gameObject);
            return hit.gameObject;
        }
        return null;
        */
    }


}
